home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / wstr.exe / WMISC.H < prev    next >
Text File  |  1993-02-24  |  6KB  |  177 lines

  1. #ifndef WMiscIncluded
  2. #define WMiscIncluded
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5. // 1916 Brooks #205, Missoula, MT  59801
  6. //
  7. // voice phone:  (406)543-7543
  8. // modem phone:  (406)543-1144 (2400N81)
  9. //  CompuServe:  72707,207
  10. //    Internet:  72707.207@CompuServe.com
  11.  
  12. // one big header file for miscelaneous odds n ends
  13. // this file is generally included by all other header files
  14.  
  15. // these types are for readability or for making sure that you
  16. // use the same size of integral storage across platforms
  17. typedef unsigned char  Bool;
  18. typedef unsigned char  Byte;
  19. typedef signed   char  SByte;
  20. typedef unsigned short Word;
  21. typedef signed   short SWord;
  22. typedef unsigned long  Long;
  23. typedef signed   long  SLong;
  24. typedef unsigned int   UInt;
  25.  
  26. //typedef Byte* BytePointer;  // I use it in a few places...
  27.  
  28. // constants related to above types.  The Size of these things will stay the
  29. // same from system to system.
  30.  
  31. const char  MaxChar  = 255;
  32. const Byte  MaxByte  = 255;
  33. const SByte MaxSByte = 127;
  34. const SByte MinSByte = (-128);
  35. const Word  MaxWord  = 0xFFFF;
  36. const SWord MaxSWord = 32767;
  37. const SWord MinSWord = 0x8000; // (-32768);
  38. #if sizeof(int)==2
  39.   const int   MaxInt   = 32767;
  40.   const int   MinInt   = 0x8000; // (-32768);
  41.   const UInt  MaxUInt  = 0xFFFF;
  42. #else
  43.   const int   MaxInt   = 0x7FFFFFFFL; // 2147483647;
  44.   const int   MinInt   = 0x80000000L; // (-2147483648);
  45.   const UInt  MaxUInt  = 0xFFFFFFFFL;
  46. #endif
  47. const SLong MaxSLong = 0x7FFFFFFFL; // 2147483647;
  48. const Long  MaxLong  = 0xFFFFFFFFL;
  49. const SLong MinSLong = 0x80000000L; // (-2147483648);
  50.  
  51. const Bool True  = 1;
  52. const Bool False = 0;
  53. const Bool Yes   = 1;
  54. const Bool No    = 0;
  55. const Bool On    = 1;
  56. const Bool Off   = 0;
  57.  
  58. /* the following have values < 0 so that they may be used as alternatives to
  59.    text placement rountines that need positive values */
  60.  
  61. const int Left=(MinInt+1);
  62. const int Center=(MinInt+2);
  63. const int Right=(MinInt+3);
  64.  
  65. const int NotFound=MinInt;
  66. const Long VecObjNotFound=MaxLong;
  67.   // used with some search functions.
  68.  
  69. /* misc. macros */
  70.  
  71. SByte  Abs(SByte  x);
  72. SWord  Abs(SWord  x);
  73. int    Abs(int    x);
  74. SLong  Abs(SLong  x);
  75. float  Abs(float  x);
  76. double Abs(double x);
  77.  
  78. Byte   Max(Byte   a, Byte b  );
  79. SByte  Max(SByte  a, SByte b );
  80. Word   Max(Word   a, Word b  );
  81. SWord  Max(SWord  a, SWord b );
  82. int    Max(int    a, int b   );
  83. UInt   Max(UInt   a, UInt b  );
  84. Long   Max(Long   a, Long b  );
  85. SLong  Max(SLong  a, SLong b );
  86. double Max(double a, double b);
  87.  
  88. Byte   Min(Byte   a, Byte b  );
  89. SByte  Min(SByte  a, SByte b );
  90. Word   Min(Word   a, Word b  );
  91. SWord  Min(SWord  a, SWord b );
  92. int    Min(int    a, int b   );
  93. UInt   Min(UInt   a, UInt b  );
  94. Long   Min(Long   a, Long b  );
  95. SLong  Min(SLong  a, SLong b );
  96. double Min(double a, double b);
  97.  
  98. Bool InRange(Byte   Val, Byte   Low, Byte   High);
  99. Bool InRange(SByte  Val, SByte  Low, SByte  High);
  100. Bool InRange(Word   Val, Word   Low, Word   High);
  101. Bool InRange(SWord  Val, SWord  Low, SWord  High);
  102. Bool InRange(int    Val, int    Low, int    High);
  103. Bool InRange(UInt   Val, UInt   Low, UInt   High);
  104. Bool InRange(Long   Val, Long   Low, Long   High);
  105. Bool InRange(SLong  Val, SLong  Low, SLong  High);
  106. Bool InRange(double Val, double Low, double High);
  107. Bool InRange(long double Val, long double Low, long double High);
  108.  
  109. SLong Round(double Val);      //  .2 => 0    .7 => 1    -.2 => 0    -.7 => -1
  110. SLong RoundUp(double Val);    //  .2 => 1    .7 => 1    -.2 => 0    -.7 => 0
  111. SLong RoundDown(double Val);  //  .2 => 0    .7 => 0    -.2 => -1   -.7 => -1
  112. SLong RoundOut(double Val);   //  .2 => 1    .7 => 1    -.2 => -1   -.7 => -1
  113. SLong RoundIn(double Val);     //  .2 => 0    .7 => 0    -.2 => 0    -.7 => 0
  114.  
  115. char ToUpper(char C);
  116. char ToLower(char C);
  117.  
  118. void PigeonHole(Long TotQuan, int ArrayLen, int A[]);
  119.   // spreads TotQuan as evenly as possible over A
  120.  
  121. Long SystemTick(); // returns the current system tick
  122.  
  123. #include <dos.h>
  124.  
  125. #ifndef MAJORBBS
  126.  
  127.   inline void Delay(int MilliSecs) {delay(MilliSecs);}
  128.  
  129.   void Sound(float Pitch,int MilliSecs);
  130.     // a little bit of control over the pc speaker
  131.  
  132.   void Beep(int Num=1);
  133.     // Num is the number of beeps ya want
  134.  
  135. #endif
  136.  
  137. #define Randomize() srand((int) time(NULL))
  138.   // Initializes (seeds) the random number generator
  139.  
  140. #define Random(Max) (int(rand() % Max))
  141.   // returns a random integer in the range of 0 to Max excluding Max
  142.  
  143. #define DebugPause(Msg)  {puts(Msg "\n"); Delay(2500); }
  144.   // prints the message and then pauses a bit.  Used in debugging
  145.  
  146. #define CopyArray(Dest,Source) memcpy(Dest,Source,sizeof(Dest))
  147.   // Dest must be something that sizeof will work on
  148.  
  149. #define CopyString(Dest,Source) (strncpy(Dest,Source,sizeof(Dest)-1),Dest[sizeof(Dest)-1]=0)
  150.   // For the type (Char*,Char*) Dest must be something that sizeof will work on
  151.  
  152. #define ClearStruct(A) memset((void*)(&A),0,sizeof(A))
  153.   // fill a struct with zeros
  154.  
  155. #define ClearArray(A) memset(A,0,sizeof(A))
  156.   /* fill an array with zeros.  Array must be defined so that sizeof will
  157.      work on it */
  158.  
  159. // Given these objects, standard CRC16 will be calculated
  160. Word CRC(void* P,Long Size);
  161. Word CRC(const char* S);
  162.  
  163. #define Loop while(1)
  164.   // structure for an infinite loop
  165.  
  166. #define For(TheVar,TheEnd)  for(TheVar=0;TheVar<TheEnd;TheVar++)
  167.   // Makes for a simpler, more readable, less error prone loop.
  168.   // Use  "For(I,10)" instead of "for(I=0;I<10;I++)"
  169.  
  170. typedef void (* VoidFuncPtr)(void);
  171. typedef Bool (* BoolFuncPtr)(void);
  172.  
  173. void FatalError(const char*);
  174.  
  175. #endif
  176.  
  177.